package basic.com.liz.learn.thread;

public class NumberFactory
{
    private int i = 0;
    
    private Object lock = new Object();
    
    private boolean created = false;
    
    public void createNum()
    {
        synchronized (lock)
        {
            if (!created)
            {
                i++;
                
                System.out.println("生产者: i = " + i);
                lock.notify();
                created = true;
            }
            else
            {
                
                try
                {
                    lock.wait(10000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                
            }
        }
    }
    
    public void consumer()
    {
        synchronized (lock)
        {
            if (created)
            {
              System.out.println(  Thread.currentThread().getName());
                System.out.println("消费者: i = " + i);
                created = false;
                try
                {
                    lock.wait(1000);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                lock.notify();
            }
            else
            {
                try
                {
                    lock.wait(1000);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            
        }
    }
    
}
